<?php

require 'lib/model/om/BaseScara.php';

class Scara extends BaseScara
{

    /**
     * Returneaza numarul de apartamente inregistrate din aceasta scara
     * @return int
     */
    public function getApartamenteCount()
    {
        $criteria = new UserCriteria('ApartamentPeer');
        $criteria->add(ApartamentPeer::SCARA_ID, $this->getId());

        $count = ApartamentPeer::doCount($criteria);

        return $count;
    }

    /**
     * Returneaza numarul
     */
    public function getNextApartamentNumber()
    {
        $criteria = new UserCriteria('ApartamentPeer');
        $criteria->add(ApartamentPeer::SCARA_ID, $this->getId());
        $criteria->addSelectColumn('MAX(' . ApartamentPeer::NUMBER . '+0)');

        $stmt = ApartamentPeer::doSelectStmt($criteria);
        $number = (int) $stmt->fetchColumn();

        return++$number;
    }

    public function hasApartamenteToAdd()
    {
        return $this->getRemainingAps() > 0;
    }

    public function cancelAddApartamente()
    {
        if ($this->getApartamenteCount())
        {
            $this->setRemainingAps(0)->save();

            return true;
        }

        return false;
    }

    public function isUndefined()
    {
        return $this->getAddress() === null;
    }

    public function isIncomplete()
    {
        return $this->isUndefined() || $this->hasApartamenteToAdd();
    }

}
